/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package github.hellocsl.gallerylayoutmanager.provider;

import github.hellocsl.gallerylayoutmanager.ResourceTable;
import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.List;

/**
 * 底部数据适配器
 *
 * @since 2021-08-09
 */
public class TestBottomProvider extends BaseItemProvider {
    private final Context mContext;
    private final List<String> listData;

    /**
     * 底部数据适配器
     *
     * @param context 上下文
     * @param listData 列表数据
     */
    public TestBottomProvider(Context context, List<String> listData) {
        this.mContext = context;
        this.listData = listData;
    }

    @Override
    public int getCount() {
        return listData.size();
    }

    @Override
    public Object getItem(int position) {
        return listData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component component, ComponentContainer componentContainer) {
        final Component cpt;
        ComponentHolder ch;
        if (component == null) {
            cpt = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_item_test_bottom, null, false);
            ch = new ComponentHolder(cpt);
            cpt.setTag(ch);
        } else {
            cpt = component;
            ch = (ComponentHolder) cpt.getTag();
        }
        ch.textContent.setText(listData.get(position));
        return cpt;
    }

    /**
     * 组件持有类
     *
     * @since 2021-08-09
     */
    private static class ComponentHolder {
        // 内容
        private final Text textContent;

        /**
         * 组件持有类
         *
         * @param component 组件布局
         */
        ComponentHolder(Component component) {
            textContent = (Text) component.findComponentById(ResourceTable.Id_text_item_test_bottom_content);
        }
    }
}
